package com.javabase.y2022.m02.day0206;

/**
 * @descriptions: Volatile
 * @author: Tom
 * @date: 2022/2/6 下午 04:47
 * @version: 1.0
 */
public class VolatileTest {
//    public static volatile int COUNTER = 0;
    public static int COUNTER = 0;
    public static void main(String[] args) {
        new ChangeListener().start();
        new ChangeMake().start();
    }
    static class ChangeListener extends Thread{
        @Override
        public void run() {
            int threadValue = COUNTER;
            while(threadValue < 5){
                if(threadValue != COUNTER){
                    System.out.println("Got Change for COUNTER:" + COUNTER + "");
                    threadValue = COUNTER;
                }
//                try{
//                    Thread.sleep(500);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
            }
        }
    }
    static class ChangeMake extends Thread{

        @Override
        public void run() {
            int threadValue = COUNTER;
            while(COUNTER < 5){
                System.out.println("Incrementing COUNTER to :" + (threadValue) + "");
                COUNTER = ++threadValue;
                try{
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
